package com.robot.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.robot.common.enums.CanEditTypeEnum;
import com.robot.common.enums.RobotScanPermissionTypeEnum;
import com.robot.dao.dto.PromptTemplateInfo;
import com.robot.dao.entity.Prologue;
import com.robot.dao.entity.Robot;
import org.springframework.beans.BeanUtils;

public class Converter {
    public static Robot  convert(RobotRepoEntity robotEntity){
        Robot res=new Robot();
        BeanUtils.copyProperties(robotEntity, res);// source target

        res.setRobotScanPermissionType(robotEntity.getRobotScanPermissionType().getValue());
        res.setCanEditType(robotEntity.getCanEditType().getValue());
        res.setPrologue(prologue2String( robotEntity.getPrologue()));
        res.setPrompt_template_info(promptTemplateInfo2String(robotEntity.getPrompt_template_info()));
        return res;
    }


    public static RobotRepoEntity convert(Robot robotDTO){
        RobotRepoEntity res=new RobotRepoEntity();
        BeanUtils.copyProperties(robotDTO, res);// source target
        res.setRobotScanPermissionType(Integer2RobotScanPermissionTypeEnum(robotDTO.getRobotScanPermissionType()));
        res.setCanEditType(Integer2CanEditType(robotDTO.getCanEditType()));
        res.setPrologue(string2Prologue(robotDTO.getPrologue()));
        res.setPrompt_template_info(string2PromptTemplateInfo(robotDTO.getPrompt_template_info()));
        return res;
    }

    private static RobotScanPermissionTypeEnum Integer2RobotScanPermissionTypeEnum(Integer value){
        return  RobotScanPermissionTypeEnum.fromValue(value);
    }

    private static CanEditTypeEnum Integer2CanEditType(Integer value){
        return CanEditTypeEnum.fromValue(value);
    }

    private static Prologue string2Prologue(String value) {
        ObjectMapper objectMapper = new ObjectMapper();
        Prologue prologue=null;
        try {
            prologue = objectMapper.readValue(value, Prologue.class);
        }catch (Exception e ){
            System.out.println("string2Prologue error "+e.getMessage());
        }
        return prologue;
    }

    private static PromptTemplateInfo string2PromptTemplateInfo(String value) {
        ObjectMapper objectMapper = new ObjectMapper();
        PromptTemplateInfo promptTemplateInfo=null;
        try {
            promptTemplateInfo = objectMapper.readValue(value, PromptTemplateInfo.class);
        }catch (Exception e ){
            System.out.println("string2PromptTemplateInfo error "+e.getMessage());
        }
        return promptTemplateInfo;
    }

    // 将Prologue对象转换为字符串的方法
    private static String prologue2String(Prologue prologue) {
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = null;
        try {
            jsonString = objectMapper.writeValueAsString(prologue);
        } catch (Exception e) {
            System.out.println("prologue2String error " + e.getMessage());
        }
        return jsonString;
    }

    // 将PromptTemplateInfo对象转换为字符串的方法
    private static String promptTemplateInfo2String(PromptTemplateInfo promptTemplateInfo) {
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = null;
        try {
            jsonString = objectMapper.writeValueAsString(promptTemplateInfo);
        } catch (Exception e) {
            System.out.println("promptTemplateInfo2String error " + e.getMessage());
        }
        return jsonString;
    }

}
